<?php
	/**
	 * This class is for working with web-services
	 * @package Classes
	 */
	class WebServices
	{
		function __construct()
		{
			require_once('NuSOAP/nusoap.php');
		}
		
		#SOAP#
		
		/**
	     * Returns response from a web-service
	     * 
	     * @param string $wsdl_address address of a wsdl file to get web-service response
	     * @param string $function_name web-service function name
	     * @param array $parameters function arguments
	     * @param array $options array with predetermined parameters (i.e. array("login" => "wso", "password" => ""))
	     * @return array response from the service
	     */
		static function RequestResponse($wsdl_address, $function_name, $parameters = array(), $options = array())
		{
			if (empty($parameters)) $parameters = array();
			
			$client = new SoapClient($wsdl_address, $options);
			try
			{	
//				eval ("\$response =  \$client->$function_name(\$parameters);");
//				Debugger::VarDump($parameters);	  	
//				$response =  $client->__soapCall($function_name, array('parameters'=>array('agent_Code'=>1, 'First_Character'=>2, 'manufacturer_Code'=>3, 'productcategory_Code'=>4)));	  		
				$response =  $client->__soapCall($function_name, array('parameters' => $parameters));	  	
			}
			catch (SoapFault $exception) 
			{
				echo $exception;
			}  
			return $response;
		}	

		
		#NuSOAP#
		
		/**
	     * Returns response from a web-service
	     * 
	     * @param string $wsdl_address address of a wsdl file to get web-service response
	     * @param string $function_name web-service function name
	     * @param array $parameters function arguments
	     * @param array $options array with predetermined parameters (i.e. array("login" => "wso", "password" => ""))
	     * @return array response from the service
	     */
		public function Call($wsdl_address, $function_name, $parameters = '', $options = array())
		{
			Logger::Debug("NuSOAP::Call $function_name started");
			if (empty($parameters)) $parameters = array();
			
			$client = new soapclient($wsdl_address, true);

			//sets credintials
			$client->setCredentials($options[username], $options[password]);
			
			$client->soap_defencoding = 'UTF-8';
			$client->decode_utf8 = false;
			
			Logger::Debug("NuSOAP::Call remote $function_name started");
			
			//eval("\$response = \$proxy->$function_name(\$parameters);");
			$response = $client->call($function_name, $parameters);
			
			Logger::Debug("NuSOAP::Call remote $function_name ended");
			
			//if (isset($response[faultcode])) Debugger::VarDump($response);
			
			Logger::Debug("NuSOAP::Call $function_name ended");
			
			return $response;
		}
		
		/**
		 * 
		 * @param $wsdl_address wsdl address
		 * @param $function_name function name
		 * @param $parameters function parameters in associative array or objects array. E.g. array('Users' => array('Login'=>'test', 'Password=>'test')) in case of object(s) or array('Users' => array('Login'=>'test', 'Password=>'test')) in case of variables.
		 * @return unknown_type
		 */
		public function XMLCall($wsdl_address, $function_name, $parameters = '', $options = array())
		{
			//parameters preparation
			if (empty($parameters)) 
			{
				$parameters = array();
			}
			else
			{
				$parameters = (array)$parameters;
			}
			
			//creates the client
			$client = new soapclient($wsdl_address, true);
			
			//sets credintials
			$client->setCredentials($options[username], $options[password]);
			
			//gets the proxy class
			$proxy = $client->getProxy();

			//soap action
			$soapAction = $proxy->operations[$function_name][soapAction];
			
			//namespace
			$namespace = $proxy->wsdl->namespaces[tns];
			
			//gets XML from parameters array
			$XML = "<$function_name xmlns='$namespace'>";
			foreach ($parameters as $name => $params)
			{
				//if object(s)
				if (is_array($params))
				{
					$XML .= "<$name>";
					foreach ($params as $key => $value)
					{
						$XML .= "<$key>$value</$key>";
					}
					$XML .= "</$name>";
				}
				//if variables
				else
				{
					$XML .= "<$name>$params</$name>";
				}
			}
			$XML .= "</$function_name>";

			//calls the service and gets response
			$msg = $client->serializeEnvelope($XML);
			
			$response = $client->send($msg, $soapAction);

			//if (isset($response[faultcode])) Debugger::VarDump($response);
			
			return $response;			
		}
	}

//	/**
//	 * This class is for working with web-services
//	 * @package Classes
//	 */
//	class WebServices
//	{
//		/**
//	     * Returns response from a web-service
//	     * 
//	     * @param string $wsdl_address address of a wsdl file to get web-service response
//	     * @param string $function_name web-service function name
//	     * @param array array with predetermined parameters (i.e. array("login" => "wso", "password" => ""))
//	     * @return array response from the service
//	     */
//		static function RequestResponse($wsdl_address, $function_name, $params = array())
//		{
//			$client = new SoapClient($wsdl_address, $params);
//			try
//			{ 
//				eval ("\$response =  \$client->$function_name();");			  	
//			}
//			catch (SoapFault $exception) 
//			{
//				echo $exception;
//			}
//			  
//			return $response;
//		}				
//	}
?>